package pcloud.org.server.model;

import java.io.File;

public class FileInfo implements java.io.Serializable
{
	//完整的绝对路径, 相当于rootPath + relativeFullPath
	private String absolutePath;
	//完整的相对路径, 相当于absoluteFullPath去除了 rootPath部分
	private String relativePath;
	//文件所属用户对应的根路径
	private String rootPath;
	//不包含任何路径的纯文件名(但是很文件扩展名)
	private String filename;
	//文件扩展名
	private String extName;
	//标识是普通文件还是文件夹
	private boolean isFolder = false;
	//文件是否存在
	private boolean exists;
	//文件创建时间
	private long creationDate;
	//最近修改时间
	private long lastModified;
	//文件长度(字节数)
	private long length;
	//校验码
	private String CRC;
	private boolean isPublic;
	//以文本表示的文件内容
	private String content=null;

	private static final long serialVersionUID = 215124042765490081L;

	public static String getPathWithoutFileName(String path)
	{
		int lastIndexOfSplitter = path.lastIndexOf('/');
		if (lastIndexOfSplitter < 0) {
			return path;
		}

		return path.substring(0, lastIndexOfSplitter + 1);
	}

	public FileInfo()
	{

	}
	
	public FileInfo(String absolutePath, String rootPath, boolean isPublic)
	{
		this.absolutePath = absolutePath;
		this.rootPath = rootPath;
		this.relativePath = absolutePath.replace(rootPath, "");
		this.isPublic = isPublic;
	}

	public FileInfo(File targetFile, String rootPath)
	{
		this(targetFile);
		this.rootPath = rootPath;
	}


	private FileInfo(File targetFile)
	{
		createFileInfoObject(targetFile);
	}

	private void createFileInfoObject(File targetFile)
	{
		this.setFilename(targetFile.getName());
		if (targetFile.isDirectory()) {
			this.setFolder(true);
		}
		this.setAbsolutePath(replaceSlash(targetFile.getPath()));

		this.setLength(targetFile.length());
		this.setLastModified(targetFile.lastModified());
	}

	public String getCRC()
	{
		return CRC;
	}

	public long getCreationDate()
	{
		return this.creationDate;
	}

	public String getExtName()
	{
		return extName;
	}

	public String getFilename()
	{
		if (filename == null && absolutePath != null) {
			filename = getFileNameFromPath(absolutePath);
		}
		return filename;
	}

	private String getFileNameFromPath(String path)
	{
		int lastIndexOfSplitter = path.lastIndexOf('/');
		if (lastIndexOfSplitter < 0) {
			return path;
		}

		return path.substring(lastIndexOfSplitter + 1);
	}

	public String getAbsolutePath()
	{
		if (absolutePath == null && relativePath != null
				&& rootPath != null) {
			absolutePath = rootPath + ((rootPath.endsWith("/") || relativePath.startsWith("/" ) )? "" : "/")
					+ relativePath;
		}
		return absolutePath;
	}

	public long getLastModified()
	{
		return lastModified;
	}

	public long getLength()
	{
		return length;
	}

	public String getRelativePath()
	{
		if (relativePath == null) {
			relativePath = absolutePath == null ? ""
								: rootPath == null ? absolutePath 
								: rootPath.equals(absolutePath) ? "" 
								: absolutePath.substring(rootPath.length() + 1);
		}
		return relativePath;
	}

	public String getRootPath()
	{
		return rootPath;
	}

	public boolean isFolder()
	{
		return isFolder;
	}

	public long lastModified()
	{
		return getLastModified();
	}

	public void setCRC(String crc)
	{
		CRC = crc;
	}

	public void setCreationDate(long creationDate)
	{
		this.creationDate = creationDate;
	}

	public void setExtName(String extName)
	{
		this.extName = extName;
	}

	public void setFilename(String filename)
	{
		this.filename = filename;
		if (absolutePath!=null) {
			this.absolutePath = getPathWithoutFileName(absolutePath) + filename;
		}
		if (relativePath!=null) {
			this.relativePath = getPathWithoutFileName(relativePath) + filename;
		}
	}

	public void setFolder(boolean isFolder)
	{
		this.isFolder = isFolder;
	}

	public void setAbsolutePath(String fullPath)
	{
		this.absolutePath = fullPath;
	}

	public void setLastModified(long lastModified)
	{
		this.lastModified = lastModified;
	}

	public void setLength(long length)
	{
		this.length = length;
	}

	public void setRelativePath(String relativeFullPath)
	{
		this.relativePath = relativeFullPath;
	}

	public void setRootPath(String rootPath)
	{
		this.rootPath = rootPath;
	}

	public String toString()
	{
		return "{FullPath: " + getAbsolutePath() + "\r\n" 
				+ "\r\n" + "RootPath: " + getRootPath() + "\r\n"
				+ "RelativePath: " + getRelativePath() + "\r\n" + "isRemote: "
				+ "isFolder: " + isFolder() + "\r\n"
				+ "FileName: " + getFilename() + "\r\n" + "FileLength: "
				+ getLength() + "\r\n" + "LastModified: " + getLastModified()
				+ "\r\n" + "CreationDate: " + getCreationDate() + "}";
	}

	private String replaceSlash(String localPath)
	{
		String path = localPath.replace('\\', '/');
		
		return path;
	}

	private static FileInfo emptyFileInfo = new FileInfo();
	public static FileInfo getEmptyFileInfo()
	{
		return emptyFileInfo;
	}
	public static boolean isEmptyFileInfo(FileInfo theFileInfo)
	{
		return theFileInfo==emptyFileInfo;
	}

	public boolean isPublic()
	{
		return isPublic;
	}

	public void setPublic(boolean b)
	{
		isPublic = b;
	}

	public void setExists(boolean exists)
	{
		this.exists = exists;
	}

	public boolean exists()
	{
		return exists;
	}

	public void setContent(String content)
	{
		this.content = content;
	}

	public String getContent()
	{
		return content;
	}


}
